//
//  RBDataBaseTool.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/8.
//

#import "RBDataBaseTool.h"
#import "FMDatabase.h"

#define XBWHTABLE_NAME @"XBWH_TableDB"

@implementation RBDataBaseTool
- (id)init{
    self = [super init];
    if (self) {
        NSString *dbFilePath = [self rb_dataFilePath];
        queue = [FMDatabaseQueue databaseQueueWithPath:dbFilePath];
    }
    return self;
}
-(NSString *)rb_dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [paths objectAtIndex:0];
    NSString *pathString = [document stringByAppendingPathComponent:XBWHTABLE_NAME];
    return pathString;
}

+(RBDataBaseTool *)shareInstance{
    static dispatch_once_t pred = 0;
    __strong static id _shareObject = nil;
    dispatch_once(&pred, ^{
        _shareObject = [[self alloc]init];
    });
    return _shareObject;
}
-(void)rb_initDataBase:(void(^)(FMDatabase *db))block{
    [queue inDatabase:^(FMDatabase *db) {
        block(db);
        NSSet *setTmp = FMDBReturnAutoreleased([[db valueForKey:@"_openResultSets"] copy]);
        for (NSValue *_value in setTmp) {
            FMResultSet *rs = (FMResultSet *)[_value pointerValue];
            [rs close];
        }
    }];
}
@end
